package com.xuan.bigdog.lib.chat.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;

import com.xuan.bigapple.lib.utils.ContextUtils;
import com.xuan.bigapple.lib.utils.ToastUtils;
import com.xuan.bigdog.R;
import com.xuan.bigdog.lib.chat.adapter.DGChatImageAdapter;
import com.xuan.bigdog.lib.chat.helper.DGChatImagesResource;
import com.xuan.bigdog.lib.widgets.BaseLayout;

/**
 * 会话聊天底部操作栏
 * 
 * @author xuan
 */
public class DGConversationBottomBarView extends BaseLayout {
	private Button selectVoiceBtn;// 语音图标
	private Button selectTextBtn;// 文本图标
	private Button voiceBtn;// 按住说话按钮
	private EditText inputEt;// 输入框
	private Button sendBtn;// 发送按钮
	private Button selectToolsBtn;// 选择拍照或者表情图标

	private View toolsRl;// 选图拍照+表情
	private GridView toolsGv;// 选图拍照
	private GridView expressionGv;// 表情

	private DGChatImagesResource imagesResource;// 资源数据

	public DGConversationBottomBarView(Context context) {
		super(context);
	}

	public DGConversationBottomBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void dgInit() {
		loadView();
		initWidgits();
	}

	/** 加载布局 */
	private void loadView() {
		inflate(getContext(), R.layout.dg_chat_conversation_bottom_bar, this);
		selectVoiceBtn = (Button) findViewById(R.id.selectVoiceBtn);
		selectTextBtn = (Button) findViewById(R.id.selectTextBtn);
		voiceBtn = (Button) findViewById(R.id.voiceBtn);
		inputEt = (EditText) findViewById(R.id.inputEt);
		sendBtn = (Button) findViewById(R.id.sendBtn);
		selectToolsBtn = (Button) findViewById(R.id.selectToolsBtn);
		toolsRl = findViewById(R.id.toolsRl);
		toolsGv = (GridView) findViewById(R.id.toolsGv);
		expressionGv = (GridView) findViewById(R.id.expressionGv);
	}

	// 初始化控件
	private void initWidgits() {
		imagesResource = new DGChatImagesResource();
		// 喇叭图标-点击进入喇叭输入
		selectVoiceBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				selectVoiceBtn.setVisibility(View.INVISIBLE);
				inputEt.setVisibility(View.INVISIBLE);
				sendBtn.setVisibility(View.INVISIBLE);
				selectTextBtn.setVisibility(View.VISIBLE);
				voiceBtn.setVisibility(View.VISIBLE);

				resetTools();
				ContextUtils.showSoftInput(getContext(), inputEt, false);
			}
		});

		// 文本图标-点击进入文本输入
		selectTextBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				selectVoiceBtn.setVisibility(View.VISIBLE);
				inputEt.setVisibility(View.VISIBLE);
				sendBtn.setVisibility(View.VISIBLE);
				selectTextBtn.setVisibility(View.INVISIBLE);
				voiceBtn.setVisibility(View.INVISIBLE);

				resetTools();
				ContextUtils.showSoftInput(getContext(), inputEt, true);
			}
		});

		// 加号图标—点击弹出选择工具或者隐藏
		selectToolsBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!ContextUtils.hasSdCard()) {
					ToastUtils.displayTextShort(getContext(),
							"没有安装SD卡时不能使用图片、语音等功能，请安装SD卡");
					return;
				}

				if (toolsRl.getVisibility() == View.GONE) {
					showTools();
				} else {
					resetTools();
				}
			}
		});

		// 初始化工具数据
		toolsGv.setAdapter(new DGChatImageAdapter(getContext(), imagesResource
				.getToolImageList()));
		toolsGv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int postion, long arg3) {
				switch (postion) {
				case 0:
					toolsRl.setVisibility(View.GONE);
					break;
				case 1:
					toolsRl.setVisibility(View.GONE);
					break;
				case 2:
					toolsGv.setVisibility(View.GONE);
					expressionGv.setVisibility(View.VISIBLE);
					// weixinBgGridView.setAdapter(new
					// ImageAdapter(ChatActivity.this,
					// ImagesResource.getBqImages()));
					toolsRl.setBackgroundColor(Color.WHITE);

					selectVoiceBtn.setVisibility(View.VISIBLE);
					selectTextBtn.setVisibility(View.INVISIBLE);
					inputEt.setVisibility(View.VISIBLE);
					voiceBtn.setVisibility(View.INVISIBLE);
					sendBtn.setVisibility(View.VISIBLE);
					inputEt.requestFocus();
					break;
				}
			}
		});

		// 初始化表情
		expressionGv.setAdapter(new DGChatImageAdapter(getContext(),
				imagesResource.getExpressionImageList()));
	}

	/** 重置工具布局 */
	private void resetTools() {
		toolsRl.setVisibility(View.GONE);
		toolsGv.setVisibility(View.VISIBLE);
		expressionGv.setVisibility(View.GONE);
		toolsRl.setBackgroundResource(R.color.dg_color_chat_conversation_bottom_bar_select_bg);
	}

	/** 显示工具布局 */
	private void showTools() {
		toolsRl.setVisibility(View.VISIBLE);
	}

}
